package com.aliya.glide

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.aliya.glide.databinding.ActivityTransitionsBinding
import com.aliya.glide.ktx.viewBindings
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory


/**
 * 演示过度动画，原理：使用了 TransitionDrawable
 */
class TransitionsActivity : AppCompatActivity() {
    companion object {
        const val TAG = "TransitionsActivity"
    }

    private val viewBindings by viewBindings(ActivityTransitionsBinding::bind)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_transitions)
        viewBindings.ivImage0.apply {
            setOnClickListener {
                Glide.with(this)
                    .load("https://up.enterdesk.com/edpic_source/53/0a/da/530adad966630fce548cd408237ff200.jpg")
                    .placeholder(R.drawable.image_placeholder)
                    .transform(RoundedCorners(dimen(R.dimen.corner_20)))
                        // 默认只有淡入，占位图不会谈出，因动画通常比图片解码耗时
                    .transition(DrawableTransitionOptions.withCrossFade(5000)).into(this)
            }
        }
        viewBindings.ivImage1.apply {
            setOnClickListener {
                // 开启交叉淡入淡出
                val factory = DrawableCrossFadeFactory.Builder(5000)
                    .setCrossFadeEnabled(true) // 开启占位图淡出
                    .build()
                Glide.with(this)
                    .load("https://up.enterdesk.com/edpic_source/53/0a/da/530adad966630fce548cd408237ff200.jpg")
                    .placeholder(R.drawable.image_placeholder)
                    .transform(RoundedCorners(dimen(R.dimen.corner_20)))
                    .transition(DrawableTransitionOptions.withCrossFade(factory)).into(this)
            }
        }

    }

    private fun dimen(id: Int) = resources.getDimensionPixelSize(id)
}